package com.gexiaobao.pigeon.ui.fragment.mine.organization.activity

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.eventViewModel
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.showMessage
import com.gexiaobao.pigeon.app.util.ImageLoaderManager
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.ActivityWatchRegistrationBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterWatchRegistration
import com.gexiaobao.pigeon.viewmodel.OrgRaceViewModel
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2024/6/1 14:33
 * @Describe    : 关赛报名
 */
class ActivityWatchRegistration : BaseActivity<OrgRaceViewModel, ActivityWatchRegistrationBinding>() {

    private var mRaceId = 0
    private var mMemberId = 0
    private var orgName = ""
    private var raceName = ""
    private val mAdapter: AdapterWatchRegistration by lazy { AdapterWatchRegistration(arrayListOf()) }

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBar.tvTitle.text = getString(R.string.Final_registration)

        mMemberId = intent.getIntExtra("memberId", 0)
        mRaceId = intent.getIntExtra("raceId", 0)
        orgName = intent.getStringExtra("orgName").toString()
        raceName = intent.getStringExtra("raceName").toString()
        val orgIcon = intent.getStringExtra("orgIcon")
        mDatabind.includeOrgTitle.tvIncludeOrgName.text = orgName
        mDatabind.includeOrgTitle.tvIncludeOrgRaceName.text = raceName
        ImageLoaderManager.loadCircleImageFall(this, orgIcon, mDatabind.includeOrgTitle.ivOrgIcon, R.mipmap.profilepic_str_1)

        mDatabind.includeList.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter)
            .addItemDecoration(SpaceItemDecoration(0, Util.dp2px(this, 10f)))

        mDatabind.includeList.swipeRefreshLayout.init {
            onRefresh()
        }
        onRefresh()

        eventViewModel.toRefreshData.observeInActivity(this) {
            onRefresh()
        }

        mAdapter.addChildClickViewIds(R.id.tvGroupProcedure, R.id.tvSignPeopleSearch, R.id.tvNowSign, R.id.tvItemPassSignUploadProof)
        mAdapter.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.tvItemPassSignUploadProof -> startActivity<ActivityPassSignAllOrder>("raceId" to mRaceId)
                R.id.tvNowSign -> {
                    if (mAdapter.data[position].status == 1) {
                        showMessage(getString(R.string.Registration_is_not_open_yet))
                        return@setOnItemChildClickListener
                    }
                    if (mAdapter.data[position].status == 3) {
                        showMessage(getString(R.string.Registration_closed))
                        return@setOnItemChildClickListener
                    }
                    startActivity<ActivitySignPassGroup>(
                        "teamName" to mAdapter.data[position].teamName,
                        "baseId" to mAdapter.data[position].baseId,
                        "memberId" to mMemberId,
                        "orderId" to mAdapter.data[position].orderId
                    )
                }

                R.id.tvSignPeopleSearch -> {}//报名人员查询
                R.id.tvGroupProcedure -> startActivity<ActivityGroupProcedure>("rule" to mAdapter.data[position].ruleFile, "title" to "赛事规程")
            }
        }
    }

    fun onRefresh() {
        if (mMemberId == 0) {
            RxToast.showToast(getString(R.string.Failed_to_query_member_info))
            return
        }
        showLoading(getString(R.string.loading))
        mViewModel.getAppBaseList(mRaceId, mMemberId)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun createObserver() {
        super.createObserver()
        /** 获取项目列表  */
        mViewModel.appBaseResult.observe(this) {
            if (mDatabind.includeList.swipeRefreshLayout.isRefreshing) {
                mDatabind.includeList.swipeRefreshLayout.isRefreshing = false
            }
            mDatabind.includeList.swipeRecyclerView.isVisible = !it.isEmpty()
            mDatabind.includeEmptyView.llEmptyData.isVisible = it.isEmpty()
            if (!it.isEmpty()) {
                mAdapter.setList(it.list)
                mAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack) {
            when (it) {
                mDatabind.includeBar.ivBack -> {
                    finish()
                }
            }
        }
    }
}